﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Xml;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using Bricks.Business.Models.Account;
using System.ComponentModel.Composition.Hosting;
using Bricks.Business.Service;
using System.Configuration;
using Bricks.Business.Service.Helper.Ioc;

namespace Bricks.Site.Web.CommonUtils
{
    public class FlowFilterAndRequestUtils
    {
        /// <summary>
        /// 判断登陆人的机构编码和用户编码是否在能做申请的机构用户字符串里
        /// </summary>
        /// <param name="requestDepartmentAndUserList">机构用户字符</param>
        /// <param name="orgCode">登陆人的机构编码</param>
        /// <param name="userName">登陆人的用户编码</param>
        /// <returns></returns>
        public static bool IsHasOrgCodeAndUserNameInStr(string requestDepartmentAndUserList, string orgCode, string userName)
        {
            string[] requestDepartmentAndUserArr = requestDepartmentAndUserList.Split(',');
            foreach (string requestDepartmentAndUser in requestDepartmentAndUserArr)
            {
                string[] departmentAndPersonArr = requestDepartmentAndUser.Split('|');
                if (departmentAndPersonArr.Length == 2)
                {
                    if (departmentAndPersonArr[0] == orgCode && departmentAndPersonArr[1] == userName)
                    {
                        return true;
                    }
                }
                else if (departmentAndPersonArr.Length == 1)
                {
                    if (departmentAndPersonArr[0] == orgCode)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// 获取指定部门的下级部门
        /// </summary>
        /// <param name="parentID">父ID</param>
        /// <param name="lstAllDepartmentOfNeedFilterContract">所有需要做合同筛选的机构列表</param>
        public static void GetSubOrgInfo(Guid parentID, ref List<Guid> lstAllDepartmentOfNeedFilterContract)
        {
            //var container = HttpContext.Current.Application["Container"] as CompositionContainer;
            //var orgInfoService = container.GetExportedValueOrDefault<IOrgInfoService>();

            DirectoryCatalog catalog = new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath);
            MefDependencySolver mef = new MefDependencySolver(catalog);
            var orgInfoService = mef.GetService(typeof(IOrgInfoService)) as IOrgInfoService;
            var lstOrgInfo = orgInfoService.Orgs.Where(o => o.PID == parentID);
            foreach (OrgInfo orgInfo in lstOrgInfo)
            {
                lstAllDepartmentOfNeedFilterContract.Add(orgInfo.ID);
                GetSubOrgInfo(orgInfo.ID, ref lstAllDepartmentOfNeedFilterContract);
            }
        }

        /// <summary>
        /// 所有需要做合同筛选的机构是否包含登陆用户所在部门
        /// </summary>
        /// <param name="lstAllDepartmentOfNeedFilterContract">所有需要做合同筛选的机构列表</param>
        /// <param name="orgs">当前用户所属的机构列表</param>
        /// <returns></returns>
        public static bool IsContainLoginOrg(List<Guid> lstAllDepartmentOfNeedFilterContract, List<OrgInfo> orgs)
        {
            foreach (OrgInfo org in orgs)
            {
                if (lstAllDepartmentOfNeedFilterContract.Contains(org.ID))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 当前用户所属的机构列表
        /// </summary>
        /// <param name="orgs"></param>
        /// <returns></returns>
        public static bool IsContainLoginOrg(List<OrgInfo> orgs)
        {
            string departmentOfViewContractStr = ConfigurationManager.AppSettings["DepartmentOfViewContract"] as string;
            string[] departmentArr = departmentOfViewContractStr.Split(',');
            List<string> lstDepartment = new List<string>();
            lstDepartment.AddRange(departmentArr);
            foreach (OrgInfo org in orgs)
            {
                if (lstDepartment.Contains(org.OrgCode))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取指定机构的下级机构
        /// </summary>
        /// <param name="parentID">指定机构ID</param>
        /// <param name="correspondenceUnitStr">机构拼接字符串</param>
        public static void GetSubCorrespondenceUnit(Guid parentID, ref string correspondenceUnitStr)
        {
            //var container = HttpContext.Current.Application["Container"] as CompositionContainer;
            //var orgInfoService = container.GetExportedValueOrDefault<IOrgInfoService>();
            //IQueryable<OrgInfo> lstOrgInfo = orgInfoService.Orgs.Where(o => o.PID == parentID);
            //foreach (OrgInfo orgInfo in lstOrgInfo)
            //{
            //    correspondenceUnitStr += "'" + orgInfo.ID.ToString() + "',";
            //    GetSubCorrespondenceUnit(orgInfo.ID, ref correspondenceUnitStr);
            //}

            DirectoryCatalog catalog = new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath);
            MefDependencySolver mef = new MefDependencySolver(catalog);
            var orgInfoService = mef.GetService(typeof(IOrgInfoService)) as IOrgInfoService;
            IQueryable<OrgInfo> lstOrgInfo = orgInfoService.Orgs.Where(o => o.PID == parentID);
            foreach (OrgInfo orgInfo in lstOrgInfo)
            {
                correspondenceUnitStr += "'" + orgInfo.ID.ToString() + "',";
                GetSubCorrespondenceUnit(orgInfo.ID, ref correspondenceUnitStr);
            }
        }
    }
}